package com.my.spvideo.service;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.my.spvideo.cache.MenuCache;
import com.my.spvideo.dao.mysql.baidumv.BusinessMapper;
import com.my.spvideo.dao.mysql.baidumv.CompanyMapper;
import com.my.spvideo.dao.mysql.baidumv.RoleMapper;
import com.my.spvideo.dao.mysql.baidumv.UserLoginMapper;
import com.my.spvideo.enums.CompanyType;
import com.my.spvideo.util.MTree;
import com.my.spvideo.util.user.SessionKeys;
import com.my.spvideo.util.user.User;
import com.my.spvideo.util.user.UserProperty;
import com.my.spvideo.vo.Company;
import com.my.spvideo.vo.Menu;
import com.my.spvideo.vo.UserLogin;

@Service
public class LoginService {
	@Autowired
	private UserLoginMapper userLoginMapper;
	@Resource
	private RoleMapper roleMapper;
	@Resource
	private CompanyMapper companyMapper;
	
	@Resource
	private BusinessMapper businessMapper;
	
	public boolean login(HttpServletRequest request,String username,String password) {
		UserLogin userLogin = userLoginMapper.login(username, password); //user表id ,roleId 
		if(userLogin==null){
			return false;
		}
		request.getSession().invalidate();
		HttpSession session = request.getSession();
		List<Integer> companyIdList = null;
		//渠道一级帐号
		if(userLogin.getRoleId()==6){
			companyIdList = companyMapper.selectUserCompanyId(userLogin.getId());
			if(companyIdList==null||companyIdList.isEmpty())
				return false;
		}
		//2级渠道帐号需求
		if(userLogin.getRoleId() == 2){
			companyIdList = companyMapper.selectTwoUserIdList(userLogin.getId());
			if(companyIdList==null||companyIdList.isEmpty())
				return false;
		}
		//商务帐号
		if(userLogin.getRoleId()==4){
			int businessId =businessMapper.selectBusinessId(userLogin.getId());
			UserProperty userProperty = new UserProperty(userLogin.getId(),userLogin.getRoleId(),businessId,companyIdList);
			User user = new User(session,userProperty);
			user.init();
			session.setAttribute("RoleId", user.getProperty().getRoleId());
		}else{
			UserProperty userProperty = new UserProperty(userLogin.getId(),userLogin.getRoleId(),0,companyIdList);
			User user = new User(session,userProperty);
			user.init();
			session.setAttribute("RoleId", user.getProperty().getRoleId());
		}
		
		//初始化菜单
//		String m = roleMapper.selectMenu(userLogin.getRoleId());
//		Map<Integer,MTree> map = new TreeMap<Integer,MTree>();
//		if(m!=null&&!m.equals("")){
//			String[] menuIds = m.split("\\|");
//			for(String menuId:menuIds){
//				int id = Integer.parseInt(menuId);
//				Menu menu= MenuCache.INSTANCE.getMenu(id);
//				if(menu.getParentId()==0){
//					MTree mTree = new MTree();
//					mTree.setMenu(menu);
//					map.put(id, mTree);
//				}else{
//					MTree mTree = map.get(menu.getParentId());
//					mTree.getChildMenu().add(menu);
//				}
//			}
//		}
//		session.setAttribute(SessionKeys.MENU_LIST, map.values());
		return true;
	}
	
}
